<?php
class Node_WidgetController extends Clu_Controller_Action_Widget
{
	
	public function init()
	{
		//require_once (APP_PATH.'/modules/node/Model/NodeService.php');
		//require_once (APP_PATH.'/modules/article/Model/Article.php');
		//require_once (APP_PATH.'/modules/place/Model/City.php');
	 	//require_once (APP_PATH.'/modules/category/Model/Category.php');

		$this->m = new node_Model_NodeService();
		$this->mArticle = new article_Model_Service();
		$this->mCity = new place_Model_City();
		$this->mCategory = new category_Model_Category();
		$this->mCategory->setTables('news');
		
		parent::init();
	}
	
	public function testLayoutAction()
	{
		
	}
	
	public function featuredBlogsHomeAction()
	{
	    //require_once (APP_PATH . "/modules/admin/Model/Weight.php");
    	$mWeight = new admin_Model_Weight();
    	//now get all the content_id from this $widgetPositionId;
    	$data['widget_position_id'] = $this->widget['id'] ;
    	$currentItems = $mWeight->searchCurrentItemsByWidgetPositionId($data);
    	$contents = array();
    	$nodeTypeInfo = $this->m->getNodeTypeInfo('blog');
		foreach ($currentItems as $item)
		{	
			$tmp = $this->m->getNodeData($nodeTypeInfo, $item['content_id'], array('get_article' => 1));
			$contents[] = $tmp['node'];
		}
//		foreach($contents as $k => $val)
//		{
//			sort($contents[$k]['comments']);
//		}
		$this->viewParams['items'] = $contents;
		/*
		foreach ($contents as $item)
		{
			$articles[] = $this->m->getData($item['article_id'], array('get_article' => 1));
		}
		$this->viewParams['articles'] = $articles;
		*/
		//get place categories
		//require_once (APP_PATH.'/modules/category/Model/Category.php');
		$this->mCategory = new category_Model_Category();
		$this->mCategory->setTables('place');
		$this->viewParams['listCategory'] = $this->mCategory->getRootType('Root_Place');
		
	}
	
	
	public function editTabMenuAction()
	{
		
		
		if(isset($_REQUEST['id']))
     		$this->viewParams['id'] = $_REQUEST['id'];
     	if(isset($_REQUEST['b']))
     		$this->viewParams['b'] = $_REQUEST['b'];
     	if($this->issetParam('node_type'))
 	    	$this->viewParams['nodeType'] = $this->getParam('node_type');
 	    	
	    if($this->issetParam('b'))
		{
			$this->viewParams['b'] = $this->getParam('b');
		}

	}
	/*
   public function blogTabMenuAction()
	{
		
 	    $this->viewParams['b'] = $this->getParam('b');
 	    die($this->viewParams['b']);
	}*/
	
	public function featuredBlogAction()
	{
		$nodeTypeInfo = $this->m->getNodeTypeInfo('blog');
		if($this->issetParam('b'))
		{
			$b = $this->getParam('b');
			if($b == 1)
				$data = array('views' => true, 'asc_or_desc' => "desc");
			else
				$data = array('comments' => true, 'asc_or_desc' => "desc");
		}
		else
		{
			$data = array('order_by' => "created", 'asc_or_desc' => "desc",
                          'views' => false, 'comments' => false,
                         );
		}
		$requestData = $this->m->prepareNodeSearchData($data, $nodeTypeInfo);
		if (!$requestData['success'])
			die($requestData['errorMessage']);
		else
			$searchData = $requestData['nodeSearchData'];
		
		$select = $this->m->search($searchData, $nodeTypeInfo);
    	$contents = $this->db->fetchAll($select);
		$this->viewParams['items'] = $contents;
	}
	
	
	
	public function latestBlogsAction()
	{
		$contentType = 'blog';
		$nodeTypeInfo = $this->m->getNodeTypeInfo($contentType);
		if($this->issetParam('b'))
		{
			$b = $this->getParam('b');
			if($b == 1)
				$data = array('views' => true, 'asc_or_desc' => "desc");
			else
				$data = array('comments' => true, 'asc_or_desc' => "desc");
		}
		else
		{
			$data = array('order_by' => "created", 'asc_or_desc' => "desc",
                          'views' => false, 'comments' => false,
                         );
		}
		$requestData = $this->m->prepareNodeSearchData($data, $nodeTypeInfo);
		
		if (!$requestData['success'])
			die($requestData['errorMessage']);
		else
			$searchData = $requestData['nodeSearchData'];
			if($this->issetParam('page')){
			   $searchData['page'] = $this->getParam('page');
			}
//		die_a($searchData);
		$select = $this->m->search($searchData, $nodeTypeInfo);
		$paginator = $this->generatePaginator($select, $searchData['page'], $searchData['items_per_page'], 7, 'Jumping');
        $searchUrl = encode_search_url($data, $contentType);
		$this->viewParams['searchUrl'] = 
			"http://" . SITE_URL . $this->widgetService->getView()->hvnUrl(array('type' => 'blog', 'action' => 'blog-home'),'node') . "?$searchUrl";
	    //the results
    	if(isset($paginator))
        {	
	       	$this->viewParams['paginator'] = $paginator;
			$this->viewParams['items'] = $paginator->getCurrentItems();
//			die_a($this->viewParams['items']);
			$articleList = array();
			$mArticle = new article_Model_Service();
			foreach ($this->viewParams['items'] as $p)
			{
				$articleList[$p['id']] = $mArticle->getArticleData($p['article_id']);
			}
			$this->viewParams['aList'] = $articleList;
       	}
       	
	}
	
	public function featuredBloggersAction()
	{
		$this->mUser  	  = new user_Model_User();
		$sql = "SELECT * FROM users ";
		$this->viewParams['user'] = $this->mUser->getAdapter()->fetchAll($sql);
	}
	
  public function bloggerCompanyAction()
	{
		$this->mUser  	  = new user_Model_User();
		$sql = "SELECT u.* FROM users u WHERE u.id IN (SELECT ur.uid FROM user_roles ur WHERE ur.rid=24 OR ur.rid=25)";
		$this->viewParams['user'] = $this->mUser->getAdapter()->fetchAll($sql);
	}
	
	public function aboutVietnamMenuAction()
	{
		$this->mCity      = new place_Model_City();
		$this->mCategory  = new category_Model_Category();
		$this->mCategory->setTables('news');
		$type =  array(2,9,12,18,25,26);
		$vietnam = $this->mCategory->getDescendants($type[0],1,2,'asc');
		$this->viewParams['vietnam'] = $vietnam;
       
		$disanvanhoa = $this->mCategory->getDescendants($type[1],1,2,'asc');
     	$this->viewParams['disanvanhoa'] = $disanvanhoa;

     	$amthuc = $this->mCategory->getDescendants($type[2],1,2,'asc');
      	$this->viewParams['amthuc'] = $amthuc;

       	$nghethuatdangian = $this->mCategory->getDescendants($type[3],1,2,'asc');
       	$this->viewParams['nghethuatdangian'] = $nghethuatdangian;
       
       	$thongtinhanhchinh = $this->mCategory->getDescendants($type[4],1,2,'asc');
       	$this->viewParams['thongtinhanhchinh'] = $thongtinhanhchinh;
       
       	$listCity = $this->mCity->findForSelect();
		$this->viewParams['listCity'] = $listCity;
	}
	
	public function relatedNewsAction()
	{
		$this->viewParams['category'] = $this->getParam('category');
		$this->viewParams['type'] = $this->getParam('type');
		$this->viewParams['id'] = $this->getParam('id');
		$nodeTypeInfo = $this->m->getNodeTypeInfo($this->viewParams['type']);
		if (!$nodeTypeInfo)
		{
			$this->renderError(null, "Node Type does not exist");
		}
		$data = array('category' => $this->viewParams['category']);
		$ret = $this->m->search($data, $nodeTypeInfo);
		$this->viewParams['result_news'] = $this->db->fetchAll($ret);
	}
	
	public function newEventAction()
	{
		//require_once (APP_PATH.'/modules/event/Model/Event.php');
		$mEvent = new event_Model_Service();
		$data = $mEvent->search(null);
		$results = $this->db->fetchAll($data);
		$this->viewParams['events'] = $results;
	}
	
	
	public function tourismGuideAction()
	{
		$sess = new Zend_Session_Namespace(CLU_NS);
		$db = $this->db;
		$sql = "SELECT *  FROM news WHERE category=26  ORDER BY id DESC limit 1";
		$this->viewParams['nList'] = $this->db->fetchAll($sql);
		//die_a($this->viewParams['nList']);
		if(isset($this->viewParams['nList'])){
		$listArticle[$this->viewParams['nList'][0]['id']]  =   $this->mArticle->getArticleData($this->viewParams['nList'][0]['article_id']);
		$listArticle[$this->viewParams['nList'][0]['id']]['article_body']  		= 		substring_n_words(strip_tags($listArticle[$this->viewParams['nList'][0]['id']]['article_body']  ,'<p ,<div <font ') ,100);
		}
		
		if(!empty($listArticle)){
			$this->viewParams['listArticle']   = $listArticle;
		}else{
			$this->viewParams['listArticle'] = array() ;
		}
	}
	
	public function shareExperiencesAction()
	{

	    $sess = new Zend_Session_Namespace(CLU_NS);
		$db = $this->db;
		$select = $db->select();
		// Add a FROM clause
        $select->from( array('n' => 'news'));
       	$select->where('n.category = ?', $db->quoteinto(26,'INTEGER'));
        $select->order(array('n.id DESC', 'id'));
//        die_a($this->db->fetchAll($select));
   		if(isset($data['item_per_page']) == null)
			$data['item_per_page'] = 10;
		if(isset($data['page']) == null)
			$data['page'] = 1 ;
        $paginator = $this->generatePaginator($select, $data['page'], $data['item_per_page'], 7, 'Jumping');
	 	    if(isset($paginator))
       {	
           	$searchUrl = encode_search_url($data, "news");
			$this->viewParams['searchUrl'] = 
			"http://" . SITE_URL . $this->widgetService->getView()->hvnUrl(array('action' => 'search', 'module' => "event"),'nocontroller') . "?$searchUrl";
	       	$this->viewParams['paginator'] = $paginator;
			$this->viewParams['nList'] = $paginator->getCurrentItems();
       }	
	foreach($this->viewParams['nList']  as $key=>$value){
            $listArticle[$value['id']]  =   $this->mArticle->getArticleData($value['article_id']);
            $listArticle[$value['id']]['article_body']  = 	substring_n_words(strip_tags($listArticle[$value['id']]['article_body']  ,'<div <span ') ,50);   
    	}
		if(!empty($listArticle)){
			$this->viewParams['listArticle']   = $listArticle;
		}else{
			$this->viewParams['listArticle'] = array() ;
		}
		
		$listCity = $this->mCity->findForSelect();
		$this->viewParams['listCity'] = $listCity;
		
	}
	
	public function heritageAction()
	{
	    $sess = new Zend_Session_Namespace(CLU_NS);
		$db = $this->db;
		$sql = "SELECT *  FROM news WHERE (category=11 OR category=10) ORDER BY id DESC limit 8";
		$this->viewParams['nList'] = $this->db->fetchAll($sql);
		foreach($this->viewParams['nList']  as $n){
            		$listArticle[$n['id']]  =   $this->mArticle->getArticleData($n['article_id']);
            	//	$listArticle[$n['id']]['article_body']	= substring_n_words(strip_tags($listArticle[$n['id']]['article_body']  ,'<p ,<div <font ') ,10);
		}
		if(!empty($listArticle)){
			        $this->viewParams['listArticle']   = $listArticle;
		}else{
			        $this->viewParams['listArticle'] = array() ;
		}
	}

	public function updatedBlastAction()
    {
    	//require_once (APP_PATH.'/modules/node/Form/Node.php');
    	$nodeTypeInfo = $this->m->getNodeTypeInfo('blast');
		$sess = new Zend_Session_Namespace(CLU_NS);
		$auth = Zend_Auth::getInstance();
		$uIdentity = $auth->getIdentity();
		$userId=substr($_SERVER['REQUEST_URI'],12);
		if(isset($uIdentity)||isset($userId))
		{
			//$userId = $uIdentity->id;
			$this->viewParams['userId'] = $userId;
			$data = array('type' => 'blast', 'poster_id' => $userId);
			$results = $this->m->search($data, $nodeTypeInfo);
			$this->viewParams['content_blast'] = $this->db->fetchRow($results);
		}
    }
    
    public function viewPromotionAction()
    {
    	$contentType = 'blog';
    	$nodeTypeInfo = $this->m->getNodeTypeInfo($contentType);
		$auth = Zend_Auth::getInstance();
		$uIdentity = $auth->getIdentity();
		if(isset($uIdentity))
		{
			$userId = $uIdentity->id;
			$data = array('poster_id' => $userId, 'asc_or_desc' => "desc");
			$requestData = $this->m->prepareNodeSearchData($data, $nodeTypeInfo);
			if (!$requestData['success'])
				die($requestData['errorMessage']);
			else
				$searchData = $requestData['nodeSearchData'];
				if($this->issetParam('page')){
				   $searchData['page'] = $this->getParam('page');
				}
			
			$this->viewParams['contentType'] = $contentType;
			$select = $this->m->search($searchData, $nodeTypeInfo);
			$paginator = $this->generatePaginator($select, $searchData['page'], $searchData['items_per_page'], 7, 'Jumping');
	        $searchUrl = encode_search_url($data, $contentType);
			$this->viewParams['searchUrl'] = 
				"http://" . SITE_URL . $this->widgetService->getView()->hvnUrl(array('type' => 'promotion', 'action' => 'search'),'node') . "?$searchUrl";
		    //the results
	    	if(isset($paginator))
	        {	
		       	$this->viewParams['paginator'] = $paginator;
				$this->viewParams['items'] = $paginator->getCurrentItems();
				
				//get articles
				//require_once(APP_PATH . "/modules/article/Model/Article.php");
				$articleList = array();
				$mArticle = new article_Model_Service();
				foreach ($this->viewParams['items'] as $p)
				{
					$articleList[$p['id']] = $mArticle->getArticleData($p['article_id']);
				}
				$this->viewParams['aList'] = $articleList;
	       	}
	       	
		}
    }
}
?>
